package com.auotc.tabhostdemo;

import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private LayoutInflater mInflater;
    private FragmentTabHost mTabHost;
    private ArrayList<TabDataBean> tabDataList = new ArrayList<>(5);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mInflater = LayoutInflater.from(this);
        mTabHost = (FragmentTabHost) this.findViewById(android.R.id.tabhost);

  /*      //第一步，初始化fTabHost, 第三个参数为内容容器
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        //第二步，初始化菜单项
        TabHost.TabSpec tabSpec = mTabHost.newTabSpec("主页");
        *//*添加菜单项布局*//*
        View view = mInflater.inflate(R.layout.tab_indicator, null);
        ImageView iconTab = (ImageView) view.findViewById(R.id.iv_tab_icon);
        TextView tvTab = (TextView) view.findViewById(R.id.tv_tab_text);
        iconTab.setImageResource(R.mipmap.icon_tab_home_light);
        tvTab.setText("主页");
        tabSpec.setIndicator(view);
        //第三步，添加菜单项和内容
        mTabHost.addTab(tabSpec, HomeFragment.class, null);*/

        initTabHost();
    }

   /* private void initTabHost() {
        //初始化fTabHost, 第三个参数为内容容器
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        *//*初始化数据源*//*
        TabDataBean bean = new TabDataBean(R.string.tabHome, R.drawable.selector_tab_home, HomeFragment.class);

        //添加底部菜单项-tabSpec
        TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(bean.getTabName()));
        //给菜单项添加内容，indicator,其中indicator需要的参数View即为菜单项的布局
        tabSpec.setIndicator(getIndicatorView(bean));
        //第二参数就是该菜单项对应的页面内容
        mTabHost.addTab(tabSpec, bean.getContent(), null);
    }*/

    /**
     * 初始化底部导航栏
     */
    private void initTabHost() {
        //初始化fTabHost, 第三个参数为内容容器
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        /*初始化数据源*/
        TabDataBean tabHome = new TabDataBean(R.string.tabHome, R.drawable.selector_tab_home, HomeFragment.class);
        TabDataBean tabHot = new TabDataBean(R.string.tabHot, R.drawable.selector_tab_hot, HotFragment.class);
        TabDataBean tabCategory = new TabDataBean(R.string.tabCategory, R.drawable.selector_tab_category, CategoryFragment.class);
        TabDataBean tabCart = new TabDataBean(R.string.tabCart, R.drawable.selector_tab_cart, CartFragment.class);
        tabDataList.add(tabHome);
        tabDataList.add(tabHot);
        tabDataList.add(tabCategory);
        tabDataList.add(tabCart);
        //添加底部菜单项-tabSpec
        for (TabDataBean bean : tabDataList) {
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(bean.getTabName()));
            //给菜单项添加内容，indicator,其中indicator需要的参数View即为菜单项的布局
            tabSpec.setIndicator(getIndicatorView(bean));
            //第二参数就是该菜单项对应的页面内容
            mTabHost.addTab(tabSpec, bean.getContent(), null);
        }
    }


    private View getIndicatorView(TabDataBean bean){
        View view = mInflater.inflate(R.layout.tab_indicator, null);
        ImageView iconTab = (ImageView) view.findViewById(R.id.iv_tab_icon);
        TextView tvTab = (TextView) view.findViewById(R.id.tv_tab_text);
        iconTab.setImageResource(bean.getTabIcon());
        tvTab.setText(bean.getTabName());
        return view;
    }

}
